/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.util.ArrayList;
import java.util.Date;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import messageClasses.PriceMessage;
import messageClasses.ShipmentMessage;
import messageClasses.StatusMessage;
import persistence.Address;
import persistence.Customer;
import persistence.CustomerFacade;
import persistence.Prices;
import persistence.PricesFacade;
import persistence.Section;
import persistence.Shipment;
import persistence.ShipmentFacade;
import persistence.Status;
import persistence.StatusFacade;
import persistence.TypePackage;
import persistence.TypePackageFacade;
import persistence.Worker;
import plains.CustomerPlain;
import plains.ShipmentPlain;
import services.BusinessInterface;



/**
 *
 * @author ceu
 */
@Stateless
@LocalBean
public class ViewFacade implements BusinessInterface {

    @EJB
    public CustomerFacade customerFacade;
    @EJB
    public ShipmentFacade shipmentFacade;
    @EJB
    public TypePackageFacade typepackageFacade;
    @EJB
    public PricesFacade pricesFacade;
    @EJB
    public StatusFacade statusFacade;

    
    
     @Override
     public boolean lanzarProcesoPrecio(String message){
         String[] split = message.split(";");
         PriceMessage priceMessage = new PriceMessage(Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]), Float.parseFloat(split[4]), Float.parseFloat(split[5]));
         int typePackage = this.getTypePackageFromWeighAndDimension(priceMessage.getWeight(),priceMessage.getDimension());
         double price =this.getPrice(priceMessage.getPoboxDestiny(), priceMessage.getPoboxOrigin(),typePackage);
         System.out.println("El precio es de: "+price);
         
         //TODO: Lanzar proceso de consultar precio
         //obtener el precio del pedido
         //crear cola temporal y enviar precio              
         return true;
     }
     
     @Override
     public boolean lanzarProcesoShipment(String message){
        String[] split = message.split(";");
        ShipmentMessage shipmentMessage = new ShipmentMessage(Float.parseFloat(split[2]), Float.parseFloat(split[3]), Integer.parseInt(split[1]), split[4], split[5], Integer.parseInt(split[6]), split[7], split[8], split[9] , Integer.parseInt(split[10]), split[11], split[12], split[13]);
          //TODO: lanzar todo el proceso de crear pedido
                            //crear el objeto envio (plain)
                            //dividirlo en secciones
                            //persistirlo
                            //crear cola temporal y enviar el numero de seguimiento
                            //a medida que avanza el tiempo cambiar el estado...                              
         return true;
     }
      
     @Override
     public boolean lanzarProcesoStatus(String message){
         String[] split = message.split(";");
         StatusMessage statusMessage = new StatusMessage(Integer.parseInt(split[1]), Integer.parseInt(split[2]));
         //CustomerPlain customer = new CustomerPlain("paco", "123", "123");
                         //viewFacade.createCustomer(customer);
                        
                        //TODO: lanzar proceso de status
                        //crear cola temporal y enviar el estado. Y que lo vean en la pagina WEB...               
         
         return true;
     }
    
    
    @Override
    public boolean createCustomer(CustomerPlain customerPlain) {
        Customer customer = new Customer(customerPlain.getName(), customerPlain.getNif(),customerPlain.getTelephone());
        customerFacade.create(customer);
        return true;
    }

    @Override
    public boolean createShipment(ShipmentPlain shipmentPlain) {
        TypePackage typePackage=new TypePackage(shipmentPlain.getTypePackage().getDescription(),shipmentPlain.getTypePackage().getType());
        ArrayList<Section> section=new ArrayList<Section>();
        
        Section s1=new Section(new Date(),new Date(), new Address(), new Address(),new Worker(), new Status(),0.0,new Integer(0),0.0);
        
        if(shipmentPlain.getOriginAddress().getPobox().equals(shipmentPlain.getDestinyAddress().getPobox())){
        section.add(s1);}
        else{
        section.add(s1);
        Section s2=new Section(new Date(),new Date(), new Address(), new Address(),new Worker(), new Status(),0.0,new Integer(0),0.0);
        Section s3=new Section(new Date(),new Date(), new Address(), new Address(),new Worker(), new Status(),0.0,new Integer(0),0.0);
        section.add(s2);
        section.add(s3);
        }
        Address addressDestiny=new Address(shipmentPlain.getDestinyAddress().getPobox(),shipmentPlain.getDestinyAddress().getCity(),shipmentPlain.getDestinyAddress().getState(),shipmentPlain.getDestinyAddress().getStreet(),shipmentPlain.getDestinyAddress().getCountry(),shipmentPlain.getDestinyAddress().getNotes());
        Address addressOrigin=new Address(shipmentPlain.getOriginAddress().getPobox(),shipmentPlain.getOriginAddress().getCity(),shipmentPlain.getOriginAddress().getState(),shipmentPlain.getOriginAddress().getStreet(),shipmentPlain.getOriginAddress().getCountry(),shipmentPlain.getOriginAddress().getNotes());
        Customer customer=new Customer(shipmentPlain.getCustomer().getName(), shipmentPlain.getCustomer().getNif(), shipmentPlain.getCustomer().getTelephone());
        Shipment shipment = new Shipment(shipmentPlain.getNumberPackages(), shipmentPlain.getVolume(), shipmentPlain.getNotes(), shipmentPlain.getTelephoneOrigin(), shipmentPlain.getTelephoneDestiny(),typePackage, addressDestiny,addressOrigin,new Status(shipmentPlain.getStatus().getDescription(),shipmentPlain.getStatus().getName()), customer, section);
        shipmentFacade.create(shipment);
        return true;
    }
     
    
    @Override
    public double getPrice(int origin, int destiny, int type_package){
        float price = (destiny-origin)*type_package/1000;
        if(price<0) price = price *(-1);
        return price;
    }
    
    @Override
    public boolean createTypesPackages(){   
        TypePackage p1 = new TypePackage("carta","menor de 100 gramos");
        TypePackage p2 = new TypePackage("paquete_pequeño","menor de 150 gramos");
        TypePackage p3 = new TypePackage("paquete_mediano","menor de 1 kg");
        TypePackage p4 = new TypePackage("paquete_grande","mayor de 1 kg");
        System.out.println("voy a persistir tipos paquetes");
        typepackageFacade.create(p1);
        typepackageFacade.create(p2);
        typepackageFacade.create(p3);
        typepackageFacade.create(p4);
        return true;
        
    }
    
     @Override
    public boolean createStatus(){  
         Status s1 = new Status("sin recoger", "el paquete aun no ha sido recibido");
         Status s2 = new Status("almacenadoo", "el paquete se encuentra en nuestros almacenes");
         Status s3 = new Status("enviando", "el paquete esta en camino");
         Status s4 = new Status("recibido", "el paquete ha llegado a su destinatario");
         System.out.println("voy a persistir status");
         statusFacade.create(s1);
         statusFacade.create(s2);
         statusFacade.create(s3);
         statusFacade.create(s4);
         
         return true;
     }
    
    
     
     
    
   /* 
    
    double getPriceBussiness(Prices p1){
        Double price = new Double (p1.getPoboxDestiny()-p1.getPoboxOrigin())*p1.getTypePackage().getIdtypePackage();
        return price.doubleValue();
    }
    */

    private int getTypePackageFromWeighAndDimension(float weight, float dimension) {
        if(weight < 0.1) return 1;
        else if(weight<0.1) return 2;
        else if (weight<1) return 3;
        else return 4;
    }
    
    
   
   
}
